package com.spzx.cart.controller;

import com.spzx.cart.service.ICartService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {

    @Autowired
    private ICartService cartService;

    /**
     * 添加购物车
     *
     * @param skuId  商品的skuId
     * @param skuNum 商品的增量
     * @return
     */
    @Operation(summary = "添加购物车")
    @RequiresLogin
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true)
                                @PathVariable("skuId") Long skuId,
                                @Parameter(name = "skuNum", description = "数量", required = true)
                                @PathVariable("skuNum") Integer skuNum) {

        cartService.addToCart(skuId, skuNum);
        return success();
    }

    /**
     * 购物车列表查询
     *
     * @return
     */
    @Operation(summary = "查询购物车")
    @RequiresLogin
    @GetMapping("/cartList")
    public AjaxResult cartList() {
        return success(cartService.getCartList());
    }

    /**
     * 删除购物车
     *
     * @return
     */
    @Operation(summary = "删除购物车")
    @RequiresLogin
    @GetMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@Parameter(name = "skuId", description = "商品skuId", required = true)
                                 @PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return success();
    }

    /**
     * 更新选中商品状态
     */
    @Operation(summary = "更新选中商品状态")
    @RequiresLogin
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@Parameter(name = "skuId", description = "商品skuId", required = true)
                                @PathVariable("skuId") Long skuId,
                                @Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true)
                                @PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return success();
    }
    /**
     * 购物车商品的全选
     */
    @Operation(summary = "更新购物车商品全部选中状态")
    @RequiresLogin
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true)
                                @PathVariable("isChecked") Integer isChecked) {
        cartService.allCheckCart(isChecked);
        return success();
    }

    /**
     * 清空购物车
     */
    @Operation(summary = "清空购物车")
    @RequiresLogin
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }
}